package com.heima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.dto.ChannelDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.entity.WmChannel;
import com.heima.dao.WmChannelDao;
import com.heima.model.wemedia.entity.WmSensitive;
import com.heima.service.WmChannelService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 自媒体频道表(WmChannel)表服务实现类
 *
 * @author makejava
 * @since 2022-04-12 15:34:45
 */
@Service("wmChannelService")
public class WmChannelServiceImpl implements WmChannelService {
    @Resource
    private WmChannelDao wmChannelDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public WmChannel queryById(Long id) {
        return this.wmChannelDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param wmChannel   筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
   /* @Override
    public Page<WmChannel> queryByPage(WmChannel wmChannel, PageRequest pageRequest) {
        long total = this.wmChannelDao.count(wmChannel);
        return new PageImpl<>(this.wmChannelDao.queryAllByLimit(wmChannel, pageRequest), pageRequest, total);
    }*/

    /**
     * 新增数据
     *
     * @param dto 实例对象
     * @return 实例对象
     */
    @Override
    public void insert(ChannelDto dto) {
        Integer status = dto.isStatus() ? 1 : 0;
        Integer isdefault = dto.isDefault() ? 1 : 0;

        WmChannel wmChannel = new WmChannel();
        BeanUtils.copyProperties(dto,wmChannel);
        wmChannel.setStatus(status);
        wmChannel.setIsDefault(isdefault);
        wmChannel.setCreatedTime(new Date());
        wmChannel.setCreatedTime(new Date());
       wmChannelDao.insert(wmChannel);
    }

    /**
     * 修改数据
     *
     * @param dto 实例对象
     * @return 实例对象
     */
    @Override
    public void update(ChannelDto dto) {
        Integer status = dto.isStatus() ? 1 : 0;
        Integer isdefault = dto.isDefault() ? 1 : 0;

        WmChannel wmChannel = new WmChannel();
        BeanUtils.copyProperties(dto,wmChannel);
        wmChannel.setStatus(status);
        wmChannel.setIsDefault(isdefault);
        wmChannel.setCreatedTime(new Date());
        wmChannel.setCreatedTime(new Date());
        wmChannelDao.update(wmChannel);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public void deleteById(Integer id) {
        wmChannelDao.deleteById(id);

    }

    /**
     * 查询所有频道
     *
     * @return
     */
    @Override
    public ResponseResult list() {

       List<WmChannel>list =wmChannelDao.queryAll();
       return ResponseResult.okResult(list);

    }
    /**
     * 查询所有频道
     * @return
     */
    @Override
    public ResponseResult list1(ChannelDto dto) {
        //1. 参数校验
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        dto.checkParam();
        //2. 封装查询条件
        //3. 执行查询
        PageHelper.startPage(dto.getPage(),dto.getSize());
        Page<WmChannel> page= wmChannelDao.queryByMap(dto);
        //4. 封装数据返回
        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        result.setData(page.getResult());
        return result;
    }
}